<?php
class MachineOrder extends DisOrder {
    /*
     * 云票务系统
     */
    const CLOUD_TICKET_SYSTEM  = 4;
    /*
     * 自助机取票
     */
    const SELF_SERVICE_MACHINE = 3;

    private function get_price($tid, $pid='')
    {
        $sql = "SELECT tprice,pid FROM uu_jq_ticket WHERE";
        $flag = 0;
        if (is_array($pid)) {
            $flag = 1;
            $sql .= ' pid IN('.implode(',', $pid).') LIMIT ' .count($pid);
        }
        else {
            $sql .= " id=$tid LIMIT 1";
        }
        $GLOBALS['le']->query($sql);
        if ($flag == 1) {
            $pid_price = array();
            while ($tmp=$GLOBALS['le']->fetch_assoc()) {
                $pid_price[$tmp['pid']] = $tmp['tprice'] * 100;
            }
            return $pid_price;
        } else {
            $GLOBALS['le']->fetch_assoc();
            return $GLOBALS['le']->f('tprice') * 100;
        }
    }
    public function order($option, $aid = 0, $debug=false)
    {
        $s = $this->s;
        $this->option = $option;
        $member = $this->m->memberID;
        $pid = $this->p->pid;

        $begintime = $option['begintime'];
        $ordername = $option['ordername'];
        $ordertel   = $option['ordertel'];
        $contacttel = $option['contacttel'];
        $series     = !empty($option['series']) ? $option['series'] : '';
        $tnum       = $option['tnum'];
        $remoteorder= $option['remoteorder'];
        $paymode    = $option['paymode'];
        //订单类型，normalOrder=>普通订单，linkOrder=>联票订单，packageOrder=>套票订单,
        $orderType = 'showOrder';
        $p_info = $this->p->pInfo();

        $endtime    = ($p_info['delaydays'] - 1) < 0 ? 0 : ($p_info['delaydays'] - 1);
        $time       = strtotime($begintime) + $endtime * 24 * 3600;
        $endtime    = date("Y-m-d", $time);
        $begintime .= " 00:00:00";
        $endtime   .= " 23:59:59";

        /*产品预订检查*/
        $totalMoney = 0;

        $account = $option['account'] ? $option['account'] : '';
        //
        if ($option['channel']==self::CLOUD_TICKET_SYSTEM) {
            $tprice              = $this->get_price($p_info['tid']);
            $option['ordermode'] = 10;
            if (!$tprice) {
                throw new DisOrderException('门市价不存在，下单失败！');
            }
        }
        elseif ($option['channel']==self::SELF_SERVICE_MACHINE) {
            $tprice = $this->p->uPrice_n($begintime, $pid, $aid, $account);
            if (is_array($tprice)) {
                throw new DisOrderException($tprice['msg']);
            }
        }
        else {
            throw new DisOrderException('该接口只支持自助机和云票务系统下单');
        }
//        echo $tprice;
//        exit;
        $totalTprice = $tprice * $tnum;
        $totalMoney += $totalTprice;

        $pcode = 0;
        $ordermode = isset($option['ordermode']) ?
            $option['ordermode'] : $this->ordermode;

        $c_infos = array();
        if (count($option['c_pids'])>0) {
            /*查看是否存在联票产品，存在的话记录联票产品下单信息*/
            if ($option['channel']==self::CLOUD_TICKET_SYSTEM) {
                $pid_tprice = $this->get_price(0, array_keys($option['c_pids']));
            }
            foreach ((array)$option['c_pids'] as $c_pid => $c_tnum) {
                if ($c_tnum > 0) {
                    $orderType = 'linkOrder';
                    $c_pid = abs($c_pid);
                    $c_about = $this->p->pInfo($c_pid);
                    if (isset($pid_tprice)) {
                        $c_tprice = $pid_tprice[$c_pid];
                        if (!$c_tprice) continue;
                    } else {
                        $c_tprice = $this->p->tPrice_n($this->sale, $begintime, $aid,
                            $c_pid, $account);
                        if (!is_numeric($c_tprice)) continue;
                    }
                    // 新接口
                    $c_infos[] = array(
                        "member" => $member,
                        "tprice" => $c_tprice,
                        "tnum" => $c_tnum,
                        "ltitle" => $c_about['ltitle'],
                        "ttitle" => $c_about['ttitle'],
                        "pid" => $c_pid,
                        "lid" => $c_about['lid'],
                        "tid" => $c_about['tid'],
                        "begintime" => $begintime,
                        "endtime" => $endtime,
                        "storage" => $c_about['storage'],
                        "storage_open" => $c_about['storage_open'],
                        "salerid" => $c_about['salerid'],
                        "ordername" => $ordername,
                        "ordertel" => $ordertel,
                        "paymode" => $paymode,
                        "ordermode" => $ordermode,
                        "apply_did" => $c_about['apply_did'],
                        "sapply_did" => $c_about['sapply_did'],
                        "contacttel" => $option['contacttel'],
                        "sfz" => $option['sfz'],
                    );
                    $totalMoney += $c_tprice * $c_tnum;
                }
            }
        }

        $smsSendAfterOrder = strlen($ordertel) == 11 ? 1 :0; //下单后发送短信0不发送1发送
        $smsSendWithOrder  = 1; //下单并发送短信0发送1不发送
        $isLnkOrder = count($c_infos); //是否联票订单
        $proInfos = $isLnkOrder ? $c_infos : array(); //联票购买
        $toProvider = 1;
        if($paymode==1){
            $toProvider = 0;
        }
        if ($paymode == 1 || $p_info['sendVoucher'] == 1) {
            //未支付或特殊设置不发送短信
            $smsSendAfterOrder = 0;
        }


        $proInfos[] = array(
            "member" => $member,
            "tprice" => $tprice,
            "tnum" => $tnum,
            "ltitle" => $p_info['ltitle'],
            "ttitle" => $p_info['ttitle'],
            "pid" => $p_info['pid'],
            "tid" => $p_info['tid'],
            "begintime" => $begintime,
            "endtime" => $endtime,
            "storage" => $p_info['storage'],
            "storage_open" => $p_info['storage_open'],
            "sapply_did" => $p_info['sapply_did']
        );

        $this->orderCheck($option, $p_info); //预订判定
        //TODO::如果是零售（散客购买),就不判断购买权限，反正都是用零售价
        if (!$this->sale) {
            $this->authCheck($proInfos, $aid); //购买权限判定
        }
        $this->storageCheck($proInfos); //库存判定
//        echo $smsSendWithOrder;exit;
//        echo "$s->Group_Order_Submit($member";exit;19
        $r = $s->Group_Order_Submit($member, $p_info['lid'], $p_info['tid'],
            $begintime, $endtime, $tprice, $tnum, $totalTprice, $ordername,
            $ordertel, "$contacttel", $p_info['salerid'], $p_info['sapply_did'],
            $paymode, $ordermode, "$remoteorder", "", "$series",
            $smsSendWithOrder, 0, $pcode, $option['sfz']);
//        var_dump($r);exit;
        $sendRut = '';
        if ($r >= 100000) {
            /*附属产品下单*/
            if ($isLnkOrder) {
                $this->subOrder($c_infos, $r);
            }
            //短信通知
            include '/var/www/html/new/d/class/OrderSmsManual.class.php';
            $smsManual = new \PFT\SMS\OrderSmsManual($s);
            $smsManual->SetParam($ordertel, $aid, $member, $p_info['tid'],
                $pid, $r, $p_info['lid']);
            if ($smsSendAfterOrder == 1) {
                $sms_ret = $smsManual->Send($p_info['ltitle'] . $p_info['ttitle'],
                    $tnum, $begintime, $endtime);
                if ($sms_ret != 100) {
                    self::log_result("短信发送失败|订单号[$r]|错误代码[{$sms_ret}]", '/var/www/html/new/d/logs/debug.txt');
//                    Log::writeLog("短信发送失败|订单号[$r]|错误代码[{$sms_ret}]", Log::ERROR_LOG_PATH);
                    $this->errHandle("短信发送失败[{$sms_ret}]", $r);
                }
            }
//            //通知供应商
            if ($toProvider==1) {
                $smsManual->OrderNotify('AFTER_SUBMIT', $proInfos, $ordername,
                        $r, $ordertel, $option['memo']);
            }
            /*记录订单额外信息*/
            $this->appendInfo($r, $option);
            return array('orderNum' => $r, 'orderType' => $orderType, 'sendRut' => $sendRut);
        }
        else {
            $this->errHandle("下单接口出错", $r);
        }
    }
}